package es.unex.meigas.core;

import java.awt.Frame;
import java.io.File;
import java.util.ArrayList;

import es.unex.meigas.dataObjects.IInputFactory;
import es.unex.meigas.dataObjects.OutputFactory;
import es.unex.meigas.equations.Equations;
import es.unex.meigas.extBase.IMeigasExtension;
import es.unex.meigas.extGIS.IGISConnection;
import es.unex.meigas.gui.DefaultGUIFactory;
import es.unex.meigas.gui.IGUIFactory;


public class Meigas {

	private static String m_sExtensionsClasses[] = {
		"es.unex.meigas.extLoadSaveProject.NewProjectExtension",
		"es.unex.meigas.extLoadSaveProject.LoadProjectExtension",
		"es.unex.meigas.extLoadSaveProject.SaveProjectExtension",
		//"es.unex.meigas.extLoadSaveProject.CloseExtension",
		"es.unex.meigas.extCalculateVolume.CalculateVolumeExtension",
		"es.unex.meigas.extDelete.DeleteExtension",
		"es.unex.meigas.extEquationEditor.EquationEditorExtension",
		"es.unex.meigas.extFillData.FillDataExtension",
		"es.unex.meigas.extIFN2.IFN2Extension",
		"es.unex.meigas.extIFN3.IFN3Extension",
		"es.unex.meigas.extImportFromLayer.ImportFromLayerExtension",
		"es.unex.meigas.extExportToShp.ExportToShpExtension",
		"es.unex.meigas.extShowErrors.ShowErrorsExtension",
		"es.unex.meigas.extRandomUtils.RandomStandExtension",
		"es.unex.meigas.extImportPlotFromLaserScanner.ImportPlotFromLaserScannerExtension",
		"es.unex.meigas.extGIS.GisConfigurationExtension",
		"es.unex.meigas.extGIS.ZoomExtension",
		"es.unex.meigas.extSpeciesEditor.SpeciesEditorExtension",
		"es.unex.meigas.extOperations.PerformOperationExtension"
		};

	private static IMeigasExtension[] m_Extensions;
	private static IInputFactory m_InputFactory;
	private static IGISConnection m_GISConnection;
	private static Frame m_MainFrame;
	private static OutputFactory m_OutputFactory;
	private static SpeciesCatalog m_SpeciesCatalog;
	private static Equations m_Equations;
	private static String m_sDataFolder;
	private static DasocraticProject m_DasocraticProject;

	private static IGUIFactory m_GUIFactory = new DefaultGUIFactory();

	public static IMeigasExtension[] getExtensions() {

		return m_Extensions;

	}

	public static Equations getEquations() {

		return m_Equations;

	}

	public static SpeciesCatalog getSpeciesCatalog() {

		return m_SpeciesCatalog;

	}

	public static String getDataFolder(){

		return m_sDataFolder;

	}

	public static void setDataFolder(String sFolder){

		m_sDataFolder = sFolder;

	}

	public static void initialize() {

		m_SpeciesCatalog = new SpeciesCatalog();
		m_Equations = new Equations();
		String sFile = Meigas.getDataFolder() + File.separator + "data" + File.separator + "eq.dbf";
		m_Equations.open(sFile);
		sFile = Meigas.getDataFolder() + File.separator + "data" + File.separator + "species.csv";
		m_SpeciesCatalog.open(sFile);
		ArrayList extensions = new ArrayList();

		for (int i = 0; i < m_sExtensionsClasses.length; i++) {
			try {
				Class clazz = Class.forName(m_sExtensionsClasses[i]);
				IMeigasExtension ext = (IMeigasExtension) clazz.newInstance();
				System.out.println(i+": Meigas loading extension... " + ext.getName());
				extensions.add(ext);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		m_Extensions = new IMeigasExtension[extensions.size()];
		for (int i = 0; i < extensions.size(); i++) {
			m_Extensions[i] = (IMeigasExtension) extensions.get(i);
		}

	}

	public static Frame getMainFrame() {

		return m_MainFrame;

	}

	public static void setMainFrame(Frame frame){

		m_MainFrame = frame;

	}

	public static IInputFactory getInputFactory() {

		return m_InputFactory;

	}

	public static void setInputFactory(IInputFactory inputFactory) {

		m_InputFactory = inputFactory;

	}

	public static OutputFactory getOutputFactory() {

		return m_OutputFactory;

	}

	public static void setOutputFactory(OutputFactory outputFactory) {

		m_OutputFactory = outputFactory;

	}

	public static IGUIFactory getGUIFactory() {

		return m_GUIFactory ;

	}

	public static void setGUIFactory(IGUIFactory guiFactory) {

		m_GUIFactory = guiFactory;

	}

	public static IGISConnection getGISConnection(){

		return m_GISConnection;

	}

	public static void setGISConnection(IGISConnection gisConnection){

		m_GISConnection = gisConnection;

	}

	public static DasocraticProject getDasocraticProject(){

		return m_DasocraticProject;

	}

	public static DasocraticProject startNewDasocraticProject(){

		m_DasocraticProject = new DasocraticProject();
		return m_DasocraticProject;
	}

}